package com.zcb.agrimarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcb.agrimarket.pojo.StudentApplyLog;
import com.zcb.agrimarket.pojo.TeacherApplyLog;
import com.zcb.agrimarket.pojo.TeacherApplyData;
import com.zcb.agrimarket.result.Result;
import com.zcb.agrimarket.service.TeacherApplyLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/TeacherApply")
@Api(tags = "教师申请教室相关接口")
@Slf4j
@CrossOrigin//$$$用于处理跨域问题
public class TeacherApplyController {

    @Autowired
    TeacherApplyLogService teacherApplyLogService;

    /**
     * 教师查看是否满足申请要求
     * @param teacherApplyLog
     * @return
     */
    @PostMapping("/check")
    @ApiOperation("教师查看是否满足申请要求")
    public Result<String> check(@RequestBody TeacherApplyLog teacherApplyLog){
        Boolean flag = teacherApplyLogService.check(teacherApplyLog);
        if(flag==true) return Result.success("成功提交申请！");
        return Result.success("暂时没有空余的教室满足课程排课安排！");
    }

    /**
     * 教师查询其本人申请的教室
     * TODO：后续添加JWT验证，从JWT中获取教师ID，并添加到条件中
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/teacherApply")
    @ApiOperation("教师查询本人的教室申请")
    public Result<Page> getAll(Integer page, Integer pageSize){
        log.info("当前页码：{}",page);
        log.info("页面大小：{}",pageSize);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<TeacherApplyLog> queryWrapper = new LambdaQueryWrapper();

        //执行查询
        teacherApplyLogService.page(pageInfo,queryWrapper);

        return Result.success(pageInfo);
    }

    /**
     * 教师修改教室借用，只能修改状态
     * @param id
     * @param state
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("教师撤回教室借用申请")
    public Result<String> update(Long id,Integer state){
        teacherApplyLogService.updateById(id,state);
        return Result.success("成功撤回教室借用申请！");
    }
}
